$(".navigation .floatleft .nav-title").css("color", "#000000");
var loseCount = parseInt(localStorage.loseCount);
var gambleCount = parseInt(localStorage.gambleCount);
console.log("LoseCount: ", loseCount);
console.log("GambleCount: ", gambleCount);
var money = parseFloat($(".navigation_top .sheader_img_eggs:eq(1) .sheader_red").text().replace(/,/g, ""));
console.log("Current money: ", money);
var warnFlag = 0;
var total;
if(loseCount == 0) {
	console.log("refreshed...");
	total = Math.floor(money / 725);
	localStorage.total = total;
}
else {
	console.log("not refreshed...");
	warnFlag = 1;
	total = parseInt(localStorage.total);
}
console.log("Total: ", total);

var remain = parseInt($("#RemainTitle span:eq(2)").text());

var times = 1;

var altTimes = 1;
if(loseCount > 0) {
	if(loseCount == 1) {
		altTimes = 3;
	}
	else if(loseCount == 2) {
		altTimes = 16;
	}
	else if(loseCount == 3) {
		altTimes = 100;
	}
	else if(loseCount == 4) {
		altTimes = 600;
	}
}
times = altTimes;

//刷新赔率 
var lid = window.location.href.substr(window.location.href.lastIndexOf("=")+1, 6);
var distMask = [1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1];
function refreshd() {
	$.ajax({
		type:"get",
		url:"pg28mode.aspx?refresh="+lid,
		error: function() {
		},
		success: function(data, textStatus) {
			var oddsList = data.split(",");
			console.log(oddsList);
			var sumFinal = 0;
			var invertOdds = new Array(28);
			var finalDist = [];
			for(var i = 0; i <= 27; i++) {
				if(distMask[i] > 0) {
					invertOdds[i] = 1 / parseFloat(oddsList[i]);
					sumFinal = sumFinal + invertOdds[i];
				}
			}
			for(var i = 0; i <= 27; i++) {
				if (distMask[i] > 0) {
					finalDist.push(Math.round(total * times / sumFinal * invertOdds[i]));
					$("#txt" + i).val(finalDist[i]);
				}
				else 
					finalDist.push(0);
			}
			location.href='javascript:for(var i = 0; i <= 27; i++) {$("#txt" + i).keyup().blur();};$("#conform_btn").click();$(".btnpane a").click(); void 0';
			/*
			setTimeout(function() {
				if(loseCount > 0)
					
			}, Math.round(Math.random()*10)*100+1000);
			*/
			console.log(finalDist);
			setTimeout(function() {
				window.location="/play/pc28.aspx";
			}, 20000);
		}
	});
}

function randomg() {
	var finalDist = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0];
	for(var i = 0; i < 3; i++) {
		finalDist[Math.round((Math.random()*1000)%28)] = 1;
	}
	for(var i = 0; i <= 27; i++) {
		if (finalDist[i] > 0) {
			$("#txt" + i).val(finalDist[i]);
		}
	}
	localStorage.refresh = "true";
	location.href='javascript:for(var i = 0; i <= 27; i++) {$("#txt" + i).keyup().blur();};$("#conform_btn").click();$(".btnpane a").click(); void 0';
}

if(loseCount > 0 || (Math.random() > 0.7 && gambleCount < 4)) {
	var st, et, d;
	localStorage.gambleCount = gambleCount + 1;
	st = new Date().getTime();
	$.ajax({
		type:"get",
		url:"pg28mode.aspx?refresh="+lid,
		error: function() {
		},
		success: function(data, textStatus) {
			et = new Date().getTime();
			d = et - st;
			console.log("Response time:", d);
			setTimeout(function() {
				//$(".touzhu1:eq(0)").click();
				refreshd();
			},
			(remain - 8)*1000 - 500 - d*3 );
		}
	});
}
else {
	var t = Math.round(Math.random()*(remain - 90)+10);
	console.log(t);
	console.log("do random");
	localStorage.gambleCount = 0;
	setTimeout( function() {
		randomg();
	}, 
	t*1000 );
}